﻿using System;
using System.Collections.Generic;
using System.Text;
using PSE.Framework.ProviderLoader.Configuration;
using System.Configuration;
using PSE.Framework.ProviderLoader;

namespace PSE.Framework.Service.Security.SecurityTokenService.ClaimsProcessor
{
    public class ClaimsProcessorFactory
    {
        private const string CONFIG_SECTION_NAME = "securitySTSProviders";
        private const string PROVIDER_NAME = "STSClaimsProcessorProvider";

        /// <summary>
        /// Retorna o provider de Autenticação
        /// </summary>
        /// <returns>Instância do provider de autenticação configurado.</returns>
        public static IClaimsProcessor LoadProvider()
        {
            try
            {
                //ler as configuracoes de provider do arquivo de configuracao do serviço
                ProviderConfigurationSection configSection = ConfigurationManager.GetSection(CONFIG_SECTION_NAME) as ProviderConfigurationSection;

                if (configSection != null)
                {
                    return ProviderLoader<IClaimsProcessor>.LoadProvider(configSection.Providers, PROVIDER_NAME);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new ConfigurationErrorsException("Could not create an instance of Authentication Provider configured.", ex);
            }
        }
    }
}
